//
//  GHBaseViewController.swift
//  GHWeibo
//
//  Created by LGH on 2017/8/3.
//  Copyright © 2017年 LGH. All rights reserved.
//

import UIKit



class GHBaseViewController: UIViewController {

    var tableView: UITableView?
    let cellId = "cellId"
    var refreshCtl: UIRefreshControl?
    var isPullUp = false
    var visitorInfo: [String: String]?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        setupUI()

        
        if GHNetworkManager.shared.userLogon {
            
            setupTableView()
            loadData()
        }else{
            
            setupVisitorView()
        }
        

    }
    
    func  loadData() {
        
        //如何子类没有重写该方法，则默认停止刷新
        refreshCtl?.endRefreshing()
    }

    private func setupUI() {
    
        navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.darkGray]
    }

    func setupTableView() {
        
        tableView = UITableView(frame: view.bounds, style: .plain)
        tableView?.dataSource = self
        tableView?.delegate = self
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
        view.addSubview(tableView!)
        
        refreshCtl = UIRefreshControl()
        refreshCtl?.addTarget(self, action: #selector(loadData), for: .valueChanged)
        tableView?.addSubview(refreshCtl!)
    }
    
    func setupVisitorView() {
        
        let visitorView = GHVisitorView(frame: self.view.bounds)
        
        view.addSubview(visitorView)
        visitorView.visitorViewInfo = visitorInfo
        visitorView.registerBtn.addTarget(self, action: #selector(login), for: .touchUpInside)
        visitorView.loginBtn.addTarget(self, action: #selector(register), for: .touchUpInside)
        
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .plain, target: self, action: #selector(register))
        
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: .plain, target: self, action: #selector(login))
        self.navigationItem.leftBarButtonItem?.tintColor = UIColor.orange
        self.navigationItem.rightBarButtonItem?.tintColor = UIColor.orange
        self.navigationItem.leftBarButtonItem?.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFont(ofSize: 16)], for: .normal)
        self.navigationItem.rightBarButtonItem?.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFont(ofSize: 16)], for: .normal)
    }
    
    func login(){
        
       print("登录")
    }
    
    func register(){
        
        print("注册")
    }
    
    
}

extension GHBaseViewController: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        
        let section = tableView.numberOfSections - 1
        let rowCount = tableView.numberOfRows(inSection: section)
        
        if indexPath.row == (rowCount - 1) && !isPullUp {
            
            isPullUp = true
            loadData()
            
        }
    }
    

}
